package com.ihd.ihardware.view.tzc.discovery.view;

import android.arch.lifecycle.AndroidViewModel;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.LinearLayout;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.ihd.ihardware.R;
import com.ihd.ihardware.databinding.ActivityBaseWebBinding;
import com.just.agentweb.AgentWeb;

import cn.wowjoy.commonlibrary.app.Constans;
import cn.wowjoy.commonlibrary.base.BaseActivity;
import cn.wowjoy.commonlibrary.utils.SPUtils;

public class BaseWebActivity extends BaseActivity<ActivityBaseWebBinding, AndroidViewModel> {
    private String title;
    private String url;
    private AgentWeb mAgentWeb;

    @Override
    protected void init(Bundle savedInstanceState) {
        mImmersionBar.keyboardEnable(true).statusBarDarkFont(true).init();
        url = getIntent().getStringExtra("url");

        binding.mtitlebar.setLeftBack(this);
//        binding.mtitlebar.addAction(new TitleBar.Action() {
//            @Override
//            public String getText() {
//                return null;
//            }
//
//            @Override
//            public int getDrawable() {
//                return R.drawable.btn_share;
//            }
//
//            @Override
//            public void performAction(View view) {
//
//            }
//        });

        String token = SPUtils.getString(Constans.GATEWAY_TOKEN, "");
        String mToken;
        if (!TextUtils.isEmpty(token)) {
            JsonObject mParam = new JsonParser().parse(token).getAsJsonObject();
            mToken = mParam.get("userToken").getAsString();
        } else
            mToken = "0";

        mAgentWeb = AgentWeb.with(this)
                .setAgentWebParent(binding.FL, new LinearLayout.LayoutParams(-1, -1))
                .useDefaultIndicator()
                .additionalHttpHeader("amazingToken", mToken)
//                .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
                .setWebChromeClient(new WebChromeClient() {
                    @Override
                    public void onReceivedTitle(WebView view, String title) {
                        binding.mtitlebar.setTitle(title);
                        super.onReceivedTitle(view, title);
                    }
                })
                .createAgentWeb()
                .ready()
                .go(url);

        mAgentWeb.getAgentWebSettings().getWebSettings().setJavaScriptEnabled(true);
        mAgentWeb.getAgentWebSettings().getWebSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        mAgentWeb.getAgentWebSettings().getWebSettings().setSupportZoom(true);
        mAgentWeb.getAgentWebSettings().getWebSettings().setUseWideViewPort(true);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_base_web;
    }

    @Override
    protected Class getViewModelClass() {
        return AndroidViewModel.class;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (mAgentWeb.handleKeyEvent(keyCode, event)) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onPause() {
        mAgentWeb.getWebLifeCycle().onPause();
        super.onPause();

    }

    @Override
    protected void onResume() {
        mAgentWeb.getWebLifeCycle().onResume();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAgentWeb.getWebLifeCycle().onDestroy();
    }
}
